/**
 * Created by Administrator on 2016/10/20.
 */
$(function(){
    html_init();
});


function html_init(){
    var site_url="http://test.vipcard.cn";
    layui.use('element', function(){
        var element = layui.element();
    });
    $(window).on('resize', function() {
        var min_width=1310-64;
        var $content = $('#iframe_content');
        $content.height($(this).height() - 70);
        $('.panel.row').css('min-height',$(this).height()-$(".crumb").height()-$(".car-height").height()-45);

         $('.middle-section.neiye ').css('min-height',$(window).height()-70);
         $('.middle-content.neiye ').css('min-height',$(window).height()-70);
       $('.panel.site-content ').css('min-height',$(window).height()-74);


        var width=$(this).width();
        if(width<=min_width){
            //$('.zhuyright').addClass('hide');
            $('.sy-content').addClass('gundong');
            $('.zhuyright_tubiao').removeClass('hide');
            $('[data_index="main"]').animate({
                right: '0'
            },100);
        }else{
            //$('.zhuyright').removeClass('hide');
            $('.sy-content').removeClass('gundong')
            $('.zhuyright_tubiao').addClass('hide');
            $('body').removeClass('show_moblie');
            $('[data_index="main"]').animate({
                right: '260'
            },100);
        }
        //$content.height($content.height());
    }).resize();
    if($(document).find('#iframe_content').length>0||(self.frameElement && self.frameElement.tagName == "IFRAME")){
        //alert(2);
    }else{
        //var url=location.href;
        //var url=site_url+"?url="+location.href;
        //location.href=url;
    }



    $(document).on('focus','[data_click="selectAll"]',function(){
        this.select();
    })

}
//初始化tree形事件
function InitTableChild(){
    Initchild();
    //初始化是否显示下一级
    $('[data_c="open"]').click(function(){
        var parent=$(this).parents('[data_c="parent"]');
        var data_open=parent.attr('data_open');
        if(data_open==1){
            data_open=0;
        }else{
            data_open=1;
        }
        parent.attr('data_open',data_open);
        Initchild();
    });

    function Initchild(){
        var list_tbody=$('#list_tbody');
        var parent=list_tbody.find('[data_c="parent"]');
        var child=list_tbody.find('[data_c="child"]');
        parent.each(function(){
            var data_open=$(this).attr('data_open');
            var data_id=$(this).attr('data_id');
            var data_child=$('[data_c="child"][data_parent_id="'+data_id+'"]');
            if(data_open==1){
                $(this).find('[data_s="open"]').show();
                $(this).find('[data_s="close"]').hide();
                data_child.show();
            }else{
                $(this).find('[data_s="open"]').hide();
                $(this).find('[data_s="close"]').show();
                data_child.hide();
            }
        });
    }
}


//初始化商品列表
function InitGoods(layer,data){
    return InitialRequest(layer,GoodsListUrl,data);
}

//初始化获取商品信息
function getGoodsInfo(layer,GoodsId){
    return InitialRequest(layer,GoodsInfoUrl,{id:GoodsId});
}

//初始化商品分类
function InitGoodsCategory(layer){
    return InitialRequest(layer,getPutongGoodsCategoryUrl,{});
}


//初始化Sku商品列表
function InitSkuGoods(layer,data){
    return InitialRequest(layer,SkuGoodsListUrl,data);
}

//初始化Sku商品列表
function getSkuGoodsInfo(layer,sku_id){
    return InitialRequest(layer,SkuGoodsInfoUrl,{sku_id:sku_id});
}




//初始化普通商品套餐
function InitGoodsPackageList(layer,data){
    return InitialRequest(layer,PackageListUrl,data);
}

//初始化普通商品套餐信息
function InitGoodsPackageInfo(layer,PackageId){
    return InitialRequest(layer,PackageInfoUrl,{PackageId:PackageId});
}




//初始化计次商品列表
function InitJiciGoods(layer,data){
    return InitialRequest(layer,JiciGoodsListUrl,data);
}

//初始化获取计次商品信息
function getJiciGoodsInfo(layer,JiciGoodsId){
    return InitialRequest(layer,JiciGoodsInfoUrl,{id:JiciGoodsId});
}

//初始化计次商品分类
function InitJiciGoodsCategory(layer){
    return InitialRequest(layer,getJiciGoodsCategoryUrl,{});
}

//初始化计次商品套餐
function InitJiciGoodsPackageList(layer,data){
    return InitialRequest(layer,JiciPackageListUrl,data);
}

//初始化计次商品套餐信息
function InitJiciGoodsPackageInfo(layer,PackageId){
    return InitialRequest(layer,JiciPackageInfoUrl,{PackageId:PackageId});
}



function getSearchCondition(layer,Conditiodata){
    var data={};
    data.SearchField=Conditiodata;
    return InitialRequest(layer,getSearchConditionUrl,data);
}



//发起请求
function InitialRequest(layer,url,data){
    var getData={};
    $.ajaxApiUrl({
        layer:layer,
        url:url,
        data:data,
        async:false,
        is_load:false,
        datafun:function(data){
            getData=data;
        }
    });
    return getData;
}

//loading发起请求
function InitialLoadingRequest(layer,url,data,datafun){
    $.ajaxApiUrl({
        layer:layer,
        url:url,
        data:data,
        async:true,
        is_load:true,
        tishi:'统计中，请稍后...',
        datafun:datafun,
    });
}



//发起请求
function YibuInitialRequest(layer,url,data,datafun,btn){
    //var getData={};
    $.ajaxApiUrl({
        layer:layer,
        url:url,
        data:data,
        //async:false,
        is_load:false,
        isVerifyAjax:true,
        datafun:datafun,
        btn:btn
    });
    //return getData;
}

function SaveInfo(layer,url,data,datafun,btn){
    $.ajaxApiUrl({
        layer: layer,
        url: url,
        data: data,
        async: false,
        is_load: false,
        isallresult:true,
        datafun: function (result) {
            if (result.code == 0) {
                layer.msg(result.message, {icon: 2});
                return false;
            }
            var data=result.data;
            layer.msg(result.message,{
                time: 2000 //不自动关闭
                ,offset:'300px'
                ,icon:1
                //,shade:0.7
            },function(index, layero){ //或者使用btn1
                datafun(data);
            });
        }
    });
}


// 数组对比
function diff_array(arr1, arr2) {
    var arr3 = [];
    for (var i = 0; i < arr1.length; i++) {
        var flag = true;
        for (var j = 0; j < arr2.length; j++) {
            if (arr2[j] == arr1[i]) {
                flag = false;
            }
        }
        if (flag) {
            arr3.push(arr1[i]);
        }
    }
    return arr3;
}
//笛卡儿积组合
function descartes(list, specData) {
    var point = {};
    var result = [];
    var pIndex = null;
    var tempCount = 0;
    var temp = [];
    for (var index in list) {
        if (typeof list[index] == 'object') {
            point[index] = {
                'parent': pIndex,
                'count': 0
            }
            pIndex = index;
        }
    }
    if (pIndex == null) {
        return list;
    }
    while (true) {
        for (var index in list) {
            tempCount = point[index]['count'];
            temp.push({
                "id": specData[index].id,
                "name": specData[index].name,
                "style": specData[index].style,
                "color": list[index][tempCount].color,
                "img": list[index][tempCount].img,
                "value": list[index][tempCount].value
            });
        }
        result.push(temp);
        temp = [];
        while (true) {
            if (point[index]['count'] + 1 >= list[index].length) {
                point[index]['count'] = 0;
                pIndex = point[index]['parent'];
                if (pIndex == null) {
                    return result;
                }
                index = pIndex;
            } else {
                point[index]['count']++;
                break;
            }
        }
    }
}


function isEmptyObject(Object) {
    var Rs=false;
    for(var i in Object){
        Rs=true;
        break;
    }
    return Rs;
}



(function ($) {
    $.fn.extend({
        insertAtCaret: function (myValue) {
            var $t = $(this)[0];
            if (document.selection) {
                this.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
            } else
            if ($t.selectionStart || $t.selectionStart == '0')
            {
                var startPos = $t.selectionStart;
                var endPos = $t.selectionEnd;
                var scrollTop = $t.scrollTop;
                $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos,$t.value.length);
                this.focus();
                $t.selectionStart = startPos + myValue.length;
                $t.selectionEnd = startPos + myValue.length;
                $t.scrollTop = scrollTop;
            } else {
                this.value += myValue;
                this.focus();
            }
        }
    })
})(jQuery);